//
//  Favorite.swift
//  hello
//
//  Created by 赵康 on 2024/11/11.
//

import Fluent
import Vapor

final class Favorite: Model, Content, @unchecked Sendable {
	static var schema: String { SchemaEnum.favorites.rawValue }
	
	@ID(key: .id)
	var id: UUID?
	
	@Parent(key: FieldKeys.user)
	var user: User
	
	@Parent(key: FieldKeys.album)
	var album: Album
	
	@OptionalField(key: FieldKeys.isDeleted)
	var isDeleted: Bool?
	
	@Timestamp(key: FieldKeys.createdAt, on: .create)
	var createdAt: Date?
	
	@Timestamp(key: FieldKeys.updatedAt, on: .update)
	var updatedAt: Date?
	
	@OptionalParent(key: FieldKeys.folder)
	var folder: Folder?
	
	@Field(key: FieldKeys.position)
	var position: Int
	
	init() { }
	
	init(id: UUID? = nil, userID: User.IDValue, albumID: Album.IDValue, folderID: Folder.IDValue? = nil, isDeleted: Bool?, createdAt: Date?, updatedAt: Date?, position: Int = 0) {
		self.id = id
		self.$user.id = userID
		self.$album.id = albumID
		if let folderID = folderID {
			self.$folder.id = folderID
		}
		self.isDeleted = isDeleted
		self.createdAt = createdAt
		self.updatedAt = updatedAt
		self.position = position
	}
}

// 收藏夹内的专辑排序选项
enum FavoritesSortOrder: String, Codable, CaseIterable {
	case titleAsc = "titleAsc"
	case titleDesc = "titleDesc"
	case playCountHighToLow = "playCountHighToLow"
	case playCountLowToHigh = "playCountLowToHigh"
	case audioCountHighToLow = "audioCountHighToLow"
	case audioCountLowToHigh = "audioCountLowToHigh"
	case dateAddedNewest = "dateAddedNewest"
	case dateAddedOldest = "dateAddedOldest"
	case custom = "custom" // 自定义顺序
}


// 更新收藏夹内专辑顺序请求模型
struct UpdateAlbumsOrderRequest: Content {
	let albumOrders: [AlbumOrderItem]
}

struct AlbumOrderItem: Content {
	let id: UUID
	let position: Int
}
